﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ProxyWrapper.UserProxy;
public partial class Employee : System.Web.UI.Page
{
    public string messagetype = "";
    public string message = "";
    private string roleId = "0";
    public ProxyWrapper.UserProxy.UserDetails UserObject { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx", false);
            }
            else
            {
                UserObject = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
                if (UserObject.RoleId == 1)
                {
                    if (Request.QueryString["mode"] != null)
                    {
                        UpdateData();
                    }
                    if(Request.QueryString["msg"]!=null)
                    {
                        message = Request.QueryString["msg"].ToString();
                        messagetype = "S";
                        if (message.ToLower().Contains("error"))
                            messagetype = "E";
                    }
                    ProxyWrapper.UserServiceWrapper _usr = new ProxyWrapper.UserServiceWrapper();
                    DataSet ds = _usr.GetAccountUsers("2");

                    DataTable Data = new DataTable();
                    if (Request.Form["query"] != null)
                    {
                        string search = Request.Form["query"].ToString();
                        DataTable dtFiltered = ds.Tables[0].AsEnumerable().Where(m => m.Field<string>("AppUserName").Contains(search) || m.Field<string>("EmailAddress").Contains(search)).OrderByDescending(m => m.Field<int>("UserId")).CopyToDataTable();
                        lstUsers.DataSource = dtFiltered;
                        lstUsers.DataBind();
                    }
                    else
                    {
                        lstUsers.DataSource = ds;
                        lstUsers.DataBind();
                    }
                    if (Request.QueryString["action"] != null && Request.QueryString["token"] != null && Request.QueryString["id"] != null)
                    {
                        int userId = Convert.ToInt32(Request.QueryString["id"].ToString());
                        ProxyWrapper.UserServiceWrapper _wrap = new ProxyWrapper.UserServiceWrapper();
                        string result = _wrap.DeleteUser(userId, UserObject.ClientId, UserObject.HashCode);
                        if (result.Equals("0"))
                        {
                            messagetype = "S";
                            message = "User " + Request.QueryString["username"].ToString() + " deleted successfully";
                        }
                        else
                        {
                            message = "Error while deleting user" + Request.QueryString["username"].ToString();
                            messagetype = "E";
                        }
                    }
                }
                else {

                    Response.Redirect("AccessDenied.aspx");
                }

            }
        }
        catch (Exception ex)
        { }
    }
    private void UpdateData()
    {
        try
        {
            string userId = Request.Form["userid"].ToString();
            string username = Request.Form["username1"].ToString();
            string fname = Request.Form["fname"] != null ? Request.Form["fname"].ToString() : "";
            string lname = Request.Form["lname"] != null ? Request.Form["lname"].ToString() : "";
            string phone = Request.Form["phone"] != null ? Request.Form["phone"].ToString() : "";
            string password = Request.Form["password"].ToString();
            string email = Request.Form["email"] != null ? Request.Form["email"].ToString() : "";
            string expiry = Request.Form["ed"] != null ? Request.Form["ed"].ToString() : "";
            string userCounts = Request.Form["deviceLimit"].ToString();
             roleId = Request.Form["userrole"]!=null?Request.Form["userrole"].ToString():"0";
            UserDetails objUser = new UserDetails()
            {
                UserId = Convert.ToInt32(userId),
                UserName = username,
                FirstName = fname,
                LastName = lname,
                PhoneNumber = phone,
                Password = password,
                EmailId = email,
                AccountExpiry = !string.IsNullOrWhiteSpace(expiry) ? Convert.ToDateTime(expiry) : DateTime.Now,
                DeviceLimit = !string.IsNullOrWhiteSpace(userCounts) ? Convert.ToInt32(userCounts) : 0,
                RoleId = Convert.ToInt32(roleId)
            };
            ProxyWrapper.UserServiceWrapper _wrapper = new ProxyWrapper.UserServiceWrapper();
            message = _wrapper.UpdateAccounts(objUser);
            messagetype = "S";
        }
        catch (Exception ex)
        {
        }
    }
    public string GetMessage()
    {
        string warning = "<div class='warning'><p></p><li>{0}</li><p></p></div>";
        string result = "<div class='success'><p></p><li>{0}</li><p></p></div>";
        if (messagetype == "E")
            return string.Format(warning, message);
        else if (messagetype == "S")
            return string.Format(result, message);
        else
            return "";

    }
}